package com.yc.chaoshi.cs_manage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.chaoshi.cs_manage.DTO.AdminDTO;
import com.yc.chaoshi.cs_manage.service.IUmsUserService;
import com.yc.common.api.CommonResult;
import com.yc.common.api.IErrorCode;
import com.yc.common.api.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping({"/umsUser"})
public class UmsUserController {
    private static final Logger log = LoggerFactory.getLogger(UmsUserController.class);
    @Resource
    IUmsUserService umsAdminService;

    /**
     * 获得管理员
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/list")
    public CommonResult list(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                             @RequestParam(value = "sreachname") String sreachname,
                             @RequestParam(value = "sreachrole") String sreachrole,
                             @RequestParam(value = "post",defaultValue = "1") Integer post
                             ) {
        Page page = this.umsAdminService.listAll(pageNum,pageSize,sreachname,sreachrole,Integer.parseInt(String.valueOf(post)));
        return CommonResult.success(page);
    }
    /**
     * 修改管理员是否启用状态
     */
    @GetMapping("/updatestatus/{id}/{status}")
    public CommonResult updatestatus(@PathVariable Integer id,@PathVariable Integer status){
        System.out.println(id);
        boolean result=this.umsAdminService.updatestatus(id,status);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改管理员
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Integer id){
        System.out.println(id);
        boolean result=this.umsAdminService.removeById(id);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 编辑管理员信息
     * @param adminDTO
     * @return
     */
    @PostMapping("edit")
    @ResponseBody
    public CommonResult edit(@RequestBody @Validated AdminDTO adminDTO){
        Boolean result=umsAdminService.edit(adminDTO);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
    @PostMapping("add")
    @ResponseBody
    public CommonResult add(@RequestBody @Validated AdminDTO adminDTO){
        Boolean result=umsAdminService.add(adminDTO);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
    @PostMapping("user_add")
    @ResponseBody
    public CommonResult user_add(@RequestBody AdminDTO adminDTO){
        Boolean result=umsAdminService.user_add(adminDTO);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
    @PostMapping("user_edit")
    @ResponseBody
    public CommonResult user_edit(@RequestBody AdminDTO adminDTO){
        Boolean result=umsAdminService.user_edit(adminDTO);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
}
